class ArticlesController < ApplicationController
	before_filter :find_category
	before_filter :admin_required, :except=>[:index, :show]
	before_filter :find_article, :only=>[:edit, :update, :destroy, :show, :destroy_image]

	def index
		@articles = @category ? @category.articles.products : Article.articles
		@tag_articles = @category ? Article.articles.find_tagged_with(@category.tag_list.join(',')) : Article.articles.find(:all, :limit=>7, :order=>"created_at DESC")
		@tags = @articles.tag_counts
	end
	
	def tag
		@products = Article.products.paginate_tagged_with(params[:id], :page=>params[:page], :per_page=>10, :order=>"created_at DESC")
		@articles = Article.articles.paginate_tagged_with(params[:id], :page=>params[:page], :per_page=>10, :order=>"created_at DESC")
	end

	def show
		@articles = @category ? @category.articles : Article.articles
		@next_article = @articles.find(:first, :conditions=>["id > ?", @article.id])
		@prev_article = @articles.find(:last, :conditions=>["id < ?", @article.id])
		@tag_articles = @category ? Article.articles.find_tagged_with(@category.tag_list.join(',')) : Article.articles.find(:all, :limit=>7, :order=>"created_at DESC")

		if @article.is_gmap?
			point = [@article.point_x, @article.point_y]
			@map = GMap.new("map_div_id")
			@map.control_init(:small_map => true, :map_type => true)
			@map.set_map_type_init(GMapType::G_HYBRID_MAP)
			@map.center_zoom_init(point, @article.zoom) #Ukraine
		
			marker = GMarker.new(point, :title => @article.title, :info_window=>"#{@article.title}")
			@map.overlay_global_init(marker, "marker_global")
			@map.overlay_init(marker)
		end
	end

	def new
	  @images = [Image.new]
		@article = @category ? @category.articles.new(:user_id => current_user.id) : current_user.articles.new
	end
	
	def edit
		@images = [Image.new]
		
		if @article.is_gmap?
			point = [@article.point_x, @article.point_y]
			@map = GMap.new("map_div_id")
			@map.control_init(:small_map => true, :map_type => true)
			@map.set_map_type_init(GMapType::G_HYBRID_MAP)
			@map.center_zoom_init(point, @article.zoom) #Ukraine
		
			marker = GMarker.new(point, :title => @article.title, :info_window=>"#{@article.title}")
			@map.overlay_global_init(marker, "marker_global")
			@map.overlay_init(marker)
		end
	end

	def create
	  @images = [Image.new]
		params[:article][:article_type] = @category ? :product : :article
		@article = @category ? @category.articles.create!(params[:article]) : current_user.articles.create!(params[:article])
		
		params[:image_attributes].each do |t|
			if !t[:uploaded_data].blank?
			  @article.images.create(t)
			end
		end if params[:image_attributes]
		
		redirect
	rescue ActiveRecord::RecordInvalid
    render :action => 'new'
	end

	def update
		@images = [Image.new]
		@article.update_attributes!(params[:article])
		
		params[:image_attributes].each do |t|
			if !t[:uploaded_data].blank?
			  @article.images.create(t)
			end
		end if params[:image_attributes]
		
		redirect
  rescue ActiveRecord::RecordInvalid
    render :action => 'edit'
	end

	def destroy
		@article.destroy
		redirect
	end
	
	def destroy_image
  	image = Image.find(params[:image_id])
  	image.destroy
  	
  	respond_to do |format|
  		format.js do
  			render :partial=>"article_images", :collection=>@article.images, :layout=>false
  		end
  	end
  end

	private 
	def find_category
		@category = Category.find(params[:category_id]) if params[:category_id]
		@category ||= nil
	end
	
	def find_article
		@article = Article.find(params[:id])
	end

	def redirect
		redirect_to @category ? category_articles_path(@category) : categories_path
	end
end
